<?php  

require_once(APPPATH.'controllers/epicentr.php');

class Files extends Epicentr {

	private $uploadpath = '../epicentr_files/';
	private $allowed_file_types = 'gif|jpg|png|doc|pdf|xls|ppt|pps';

	public function Files() {
		parent::Epicentr();
		
	}
	
	public function attach($jobid) {
		$this->load->model('jobsmodel');
    	$this->load->model('filesmodel');
    	
    	$job = $this->jobsmodel->retrieve_by_pkey($jobid);
		
		if ($job) {
			
			$count = 0;
			
			$data = array();
			
			$config['upload_path'] = $this->uploadpath;
			$config['allowed_types'] = $this->allowed_file_types;
			
			$this->load->library('upload', $config);
			do {
				$fieldname = "fileupload";
				if ($count > 0 ) $fieldname.= $count;
				$count ++;
				
				echo $fieldname;
				
				$fileUploaded = $this->upload->do_upload($fieldname);
				$uploadData = $this->upload->data();
				
				if ($fileUploaded) {
					$file_data = null;
	
					$file_data->jobid = $job->id;
	
					$file_data->title = $uploadData['file_name'];
					$file_data->file_name = $uploadData['file_name'];
					$file_data->file_type = $uploadData['file_type'];
					$file_data->file_path = $uploadData['file_path'];
					$file_data->file_size = $uploadData['file_size'];
					
					$file_data->datecreated = date('Y-m-d H:i:s');
					$file_data->creatorid = $this->authentication->getUserId();
				
					$this->filesmodel->add($file_data);
					
				} else echo $this->upload->display_errors();
			} while ($fileUploaded);
		}
		    	
    	redirect('jobs/showUpdates/'.$jobid);
	}
	
	public function download($fileid) {
    	$this->load->model('filesmodel');
    	
    	$file_data = $this->filesmodel->retrieve_by_pkey($fileid);
    	
    	//check if its here or on s3
    	if ($file_data->ons3) {
    		$this->load->library('s3');
    		
    		//$location = $this->s3->getPrivateURL($this->s3bucket, $imagedata->filename);
    		
    		//$this->s3->getObjectInfo($this->s3bucket, $imagedata->filename);
    		
    		$data = $this->s3->getObject($file_data->file_name, $this->s3bucket);
    		
    		$ct = $this->s3->getResponseContentType();
    		$cl = $this->s3->getResponseContentLength();
    		
    		//header("Location: ".$s3URL, TRUE, 307);
    		header("Content-Type: ".$ct);
			header("Content-Disposition: attachment; filename=\"".basename($file_data->file_name)."\";" );
			header("Content-Transfer-Encoding: binary");
			header("Content-Length: ".$cl);
			
			flush();
			
			echo $data;
    	} else {
			$this->load->helper('file');
			
			header("Content-Type: ".get_mime_by_extension($file_data->file_path.$file_data->file_name));
			header("Content-Disposition: attachment; filename=\"".basename($file_data->file_path.$file_data->file_name)."\";" );
			header("Content-Transfer-Encoding: binary");
			header("Content-Length: ".filesize($file_data->file_path.$file_data->file_name));
			$data = file_get_contents($file_data->file_path.$file_data->file_name);
			echo $data;
    	}
    	exit();
    }
	
	public function makePrivate($fileid) {
		$this->load->model('filesmodel');
		
		$file = $this->filesmodel->retrieve_by_pkey($fileid);
		
		$this->filesmodel->modify($fileid, array('public' => 0) );
		
		redirect('jobs/showUpdates/'.$file->jobid);
	}
	
	public function makePublic($fileid) {
		$this->load->model('filesmodel');
		
		$file = $this->filesmodel->retrieve_by_pkey($fileid);
		
		$this->filesmodel->modify($fileid, array('public' => 1) );
		
		redirect('jobs/showUpdates/'.$file->jobid);
	}
}

?>